﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace delge_squirreleditor
{
    /// <summary>
    /// Interaction logic for EditableTextBlock.xaml
    /// </summary>
    public partial class EditableTextBlock : UserControl
    {
        private string lastText;

        public delegate void FinishedEditingEventHandler(object sender, EventArgs e);

        public event FinishedEditingEventHandler FinishedEditing;

        public EditableTextBlock()
        {
            InitializeComponent();

            Focusable = true;
            FocusVisualStyle = null;
        }

        public string Text
        {
            get { return ((string)GetValue(TextProperty)); }
            set { SetValue(TextProperty, value); }
        }

        public bool IsEditable
        {
            get { return ((bool)GetValue(IsEditableProperty)); }
            set { SetValue(IsEditableProperty, value); }
        }

        public bool IsEditing
        {
            get { return ((bool)GetValue(IsEditingProperty)); }
            set
            {
                if (IsEditable)
                {
                    if (value)
                        lastText = Text;
                    SetValue(IsEditingProperty, value);
                }
            }
        }

        public ImageSource ImageSource
        {
            get { return ((System.Windows.Media.ImageSource)GetValue(ImageSourceProperty)); }
            set { SetValue(ImageSourceProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =
                               DependencyProperty.Register(
                               "Text",
                               typeof(string),
                               typeof(EditableTextBlock),
                               new PropertyMetadata(""));

        public static readonly DependencyProperty IsEditableProperty =
                               DependencyProperty.Register(
                               "IsEditable",
                               typeof(bool),
                               typeof(EditableTextBlock),
                               new PropertyMetadata(true));

        public static readonly DependencyProperty IsEditingProperty =
                               DependencyProperty.Register(
                               "IsEditing",
                               typeof(bool),
                               typeof(EditableTextBlock),
                               new PropertyMetadata(false));

        public static readonly DependencyProperty ImageSourceProperty =
                               DependencyProperty.Register(
                               "ImageSource",
                               typeof(ImageSource),
                               typeof(EditableTextBlock),
                               new PropertyMetadata(null));

        void TextBox_Loaded(object sender, RoutedEventArgs e)
        {
            TextBox textbox = sender as TextBox;
            textbox.Focus();
            textbox.SelectAll();
        }

        void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            string errorMessage = "";
            if (Text == "")
                errorMessage = "You must enter a name";
            else if (Text.Length > 25)
                errorMessage = "Name entered is to long";
            else if (!Project.IsVaildFilename(Text))
                errorMessage = "Name is not vaild";

            if (errorMessage != "")
                MessageBox.Show(errorMessage, "Delge squirrel editor", MessageBoxButton.OK, MessageBoxImage.Error);
            else
            {
                IsEditing = false;
                OnFinishedEditing(EventArgs.Empty);
            }
        }

        void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Enter:
                    string errorMessage = "";
                    if (Text == "")
                        errorMessage = "You must enter a name";
                    else if (Text.Length > 25)
                        errorMessage = "Name entered is to long";
                    else if (!Project.IsVaildFilename(Text))
                        errorMessage = "Name is not vaild";

                    if (errorMessage != "")
                        MessageBox.Show(errorMessage, "Delge squirrel editor", MessageBoxButton.OK, MessageBoxImage.Error);
                    else
                    {
                        IsEditing = false;
                        OnFinishedEditing(EventArgs.Empty);
                    }

                    e.Handled = true;
                    break;

                case Key.Escape:
                    IsEditing = false;
                    Text = lastText;
                    OnFinishedEditing(EventArgs.Empty);
                    e.Handled = true;
                    break;
            }
        }

        protected virtual void OnFinishedEditing(EventArgs e)
        {
            if (FinishedEditing != null) FinishedEditing(this, e);
        }
    }
}
